package com.fingard.dsp.bank.directbank.ceb02;

import com.fingard.dsp.bank.directbank.bankInterface.IOrderMsg;
import com.fingard.dsp.bank.reqrespobj.ordermsg.ReqOrderMsgQry;
import com.fingard.dsp.bank.reqrespobj.ordermsg.RetAtsOrderMsgQry;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * 
 * @author F
 *
 */
public class CEB02OrderMsg extends CEB02Base implements IOrderMsg{


    @Override
    public RetAtsOrderMsgQry pushOrderMsg(ReqOrderMsgQry p_orderMsg) throws Exception {
    	RetAtsOrderMsgQry retAts = new RetAtsOrderMsgQry(p_orderMsg.reqHeader);
        retAts.writeStart();
        String tmpRespCode = "";
        String tmpRespInfo = "";
        try {               	
            CEB02Header bankHeader = new CEB02Header();
            //银行获取相关信息
            bankHeader.FTranCode = "G056";
            XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
            //写入通用的头部节点
            writeCommonHeader_3(xmlWriter, bankHeader); 
            //写入body
            xmlWriter.writeStartElement("Body");
            xmlWriter.writeElementString("CHANNEL_CODE", bankFront.org);//
            xmlWriter.writeElementString("APPLY_ID",p_orderMsg.DetailId);//申请单ID p_orderMsg.APPLY_ID
            xmlWriter.writeElementString("MANUFACTOR_CODE",p_orderMsg.MANUFACTOR_CODE );//厂家编码 p_orderMsg.MANUFACTOR_CODE
            xmlWriter.writeElementString("DEALER_CODE",p_orderMsg.DealerCode);//接入机构号
            xmlWriter.writeElementString("DEALER_NAME",p_orderMsg.DealerName);//接入机构名称 bankFront.corporation
            xmlWriter.writeElementString("XSDJHJ",p_orderMsg.OrderTotallAmount );//订单金额合计 p_orderMsg.XSDJHJ
            xmlWriter.writeElementString("XSSQSL",p_orderMsg.OrderNum);//申请总数量 p_orderMsg.XSSQSL
            xmlWriter.writeElementString("XSSQRQ",p_orderMsg.OrderConfirmDate );//申请日期  YYYYMMDD p_orderMsg.XSSQRQ
            xmlWriter.writeElementString("CLJG",p_orderMsg.PickupPrice);//申请价格 p_orderMsg.CLJG
            xmlWriter.writeElementString("ORDER_NO",p_orderMsg.OrderCode);//采购单单号 p_orderMsg.ORDER_NO
            xmlWriter.writeElementString("CAR_BRAND_CODE",p_orderMsg.CarTypeCode);//品牌 p_orderMsg.CAR_BRAND_CODE
            xmlWriter.writeElementString("BILL_INFO_NO",p_orderMsg.BillNo );//票据号 p_orderMsg.BILL_INFO_NO
            xmlWriter.writeEndElement();
            xmlWriter.writeEndDocument();
            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(),bankHeader);//发送银行
            if(tmpStrRet[0].length() == 0){
                StringBuilder tmpSbMsg = new StringBuilder();
                //解析银行返回
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if(tmpBkReader != null){
                	String tmpRetCode = tmpBkReader.getFirstText("/Out/Head/ErrCode");
                	String tmpMsg = tmpBkReader.getFirstText("/Out/Body/ErrorInfo");
                	if (tmpRetCode.length()==0) {
                    while (tmpBkReader.read("/Out/Body")){
                        String tmpReturnCode = tmpBkReader.readedXElement.getSingleTextNotNull("RESULT_FLAG");
                        String tmpReturnMsg = tmpBkReader.readedXElement.getSingleTextNotNull("MSG");
                        if ("1".equals(tmpReturnCode)) {                        
                        	tmpRespCode = "0";
                        	tmpRespInfo = "处理成功";
                        } else {
                            tmpRespCode = "-1";
                            tmpRespInfo = tmpReturnCode +" : "+tmpReturnMsg;
                        }
                      }
                    } else {
                    	tmpRespCode = "-1";
                        tmpRespInfo = tmpMsg;
                    }
                } else {
                    tmpRespCode = "-1";
                    tmpRespInfo = tmpSbMsg.toString();
                }
            } else {
                tmpRespCode = "-1";
                tmpRespInfo = tmpStrRet[1];
            }
        } catch (Exception e) {
            tmpRespCode = "-1";
            tmpRespInfo = e.getMessage();
            e.printStackTrace();
        }
        retAts.writeRespCode(tmpRespCode);
        retAts.writeRespInfo(tmpRespInfo);
        retAts.writeEnd();
        return retAts;
    }


}
